<%--
  Created by IntelliJ IDEA.
  User: hp
  Date: 2025/11/7
  Time: 20:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="model.Employee" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="model.User" %>
<html>
<head>
    <title>员工列表</title>
    <link rel="stylesheet" href="<%= request.getContextPath() %>/css/style.css">
</head>
<body>
<div class="navbar">
    <div class="logo">员工信息管理系统</div>
    <div class="user-info">
        <%
            User loginUser = (User) session.getAttribute("loginUser");
            String username = loginUser != null ? loginUser.getUsername() : "";
        %>
        欢迎您，<%= username %> |
        <a href="<%= request.getContextPath() %>/LogoutServlet">退出登录</a>
    </div>
</div>
<div class="container">
    <div class="sidebar">
        <ul>
            <li><a href="<%= request.getContextPath() %>/DashboardServlet">仪表盘</a></li>
            <li><a href="<%= request.getContextPath() %>/EmployeeServlet?action=list" class="active">员工管理</a></li>
        </ul>
    </div>
    <div class="content">
        <div class="content-header">
            <h3>员工列表</h3>
            <a href="<%= request.getContextPath() %>/EmployeeServlet?action=add" class="btn-add">添加员工</a>
        </div>

        <!-- 多条件查询表单 -->
        <div class="search-form">
            <form action="<%= request.getContextPath() %>/EmployeeServlet?action=search" method="get">
                <%
                    String nameParam = request.getParameter("name");
                    String nameValue = nameParam != null ? nameParam : "";

                    String deptParam = request.getParameter("department");
                    String deptTechSelected = "技术部".equals(deptParam) ? "selected" : "";
                    String deptMarketSelected = "市场部".equals(deptParam) ? "selected" : "";
                    String deptHrSelected = "人事部".equals(deptParam) ? "selected" : "";

                    String posParam = request.getParameter("position");
                    String posDevSelected = "软件工程师".equals(posParam) ? "selected" : "";
                    String posMarketSelected = "市场专员".equals(posParam) ? "selected" : "";
                    String posHrSelected = "HR经理".equals(posParam) ? "selected" : "";
                %>
                <input type="text" name="name" placeholder="员工姓名" value="<%= nameValue %>">
                <select name="department">
                    <option value="">所有部门</option>
                    <option value="技术部" <%= deptTechSelected %>>技术部</option>
                    <option value="市场部" <%= deptMarketSelected %>>市场部</option>
                    <option value="人事部" <%= deptHrSelected %>>人事部</option>
                </select>
                <select name="position">
                    <option value="">所有职位</option>
                    <option value="软件工程师" <%= posDevSelected %>>软件工程师</option>
                    <option value="市场专员" <%= posMarketSelected %>>市场专员</option>
                    <option value="HR经理" <%= posHrSelected %>>HR经理</option>
                </select>
                <button type="submit" class="btn-search">查询</button>
            </form>
        </div>

        <table class="data-table">
            <thead>
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>部门</th>
                <th>职位</th>
                <th>薪资</th>
                <th>入职日期</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <%
                List<Employee> employees = (List<Employee>) request.getAttribute("employees");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String contextPath = request.getContextPath();

                if (employees != null && !employees.isEmpty()) {
                    for (Employee emp : employees) {
                        int empId = emp.getId();
                        String empName = emp.getName();
                        String empDept = emp.getDepartment();
                        String empPos = emp.getPosition();
                        BigDecimal empSalary = emp.getSalary();
                        String empHireDate = sdf.format(emp.getHireDate());

                        String viewUrl = contextPath + "/EmployeeServlet?action=view&id=" + empId;
                        String editUrl = contextPath + "/EmployeeServlet?action=edit&id=" + empId;
                        String deleteUrl = contextPath + "/EmployeeServlet?action=delete&id=" + empId;
            %>
            <tr>
                <td><%= empId %></td>
                <td><%= empName %></td>
                <td><%= empDept %></td>
                <td><%= empPos %></td>
                <td><%= String.format("%.2f", empSalary) %></td>
                <td><%= empHireDate %></td>
                <td class="action-buttons">
                    <a href="<%= viewUrl %>" class="btn-view">查看</a>
                    <a href="<%= editUrl %>" class="btn-edit">编辑</a>
                    <a href="<%= deleteUrl %>" class="btn-delete" onclick="return confirm('确定要删除吗？')">删除</a>
                </td>
            </tr>
            <%
                }
            } else {
            %>
            <tr>
                <td colspan="7" class="no-data">暂无员工数据</td>
            </tr>
            <%
                }
            %>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>